import notification from "@/services/notification";
import { observable, action,makeObservable } from 'mobx';
import ContractSeal from "@/services/contractseal";
import Contracts from "@/services/contracts";
import ContractSealModel from "@/store/models/ContractSealModel";
import GlobalFunctions from '@/store/globalFunctions';
import Workflow from "@/services/workflow";
import CheckDialog from "@/components/workflow/CheckDialog";
import { currentUser } from "@/services/auth";
import ContractApply from "@/services/contractapply";
import moment from "moment";

class ContractSealStore {
  @observable contractSealModel = new ContractSealModel(0);
  constructor() {
    makeObservable(this)
  }
  @action initStore(id) {
    this.init(id);
  }

  init(id) {
    id=id+'';
    if (id !== "0" && id !== 0 && id.substr(0,3) !== "daz") {
      ContractSeal.get({ id: id }).then(data => {
        this.contractSealModel = ContractSealModel.fromJS(data);
        Workflow.edit({ bill: "contractseal", billtype: 0, billid: id, userid: currentUser.id }).then(data1 => {
          if (data1.length > 0) {
            this.contractSealModel.setBilledit(data1[0]);
          }
        }).catch(error => {
        });
        
      }).catch(error => {
        this.contractSealModel = new ContractSealModel(id);
        this.getcode("contractseal",moment(),0,"code");
      });
    } else {
      ContractSeal.getid({ id: 0 }).then(data => {
        this.contractSealModel = new ContractSealModel(data[0].id);
        if(id.substr(0,3)==="daz"){
          let contractid=parseInt(id.substr(3));
          Contracts.get({id: contractid}).then(datas => {
            if (datas.id > 0) {
              this.contractSealModel.setContractsid(datas);
            }
          })
        }
      });
    }
  }
  getcode(tblname,bill_date,type,tblfield) {
    ContractApply.getcode( {tblname: tblname,bill_date: bill_date,type: type,tblfield: tblfield,}).then(data => {
      this.contractSealModel.setCode(data[0].code);
    }).catch(error => {
      notification.error(error.message, error.detailedMessage);
    });
  };

  dataSave() {
    if(!currentUser.isDefault){
      notification.error("没有权限.");
      return false;
    }
    if(this.contractSealModel.status ===0 && currentUser.id !==this.contractSealModel.userid){
      notification.error("草稿状态，不是制单人，不能保存！");
      return;
    }

    if(GlobalFunctions.isEmptyobj(this.contractSealModel.contractsid)){
      notification.error("合同不能为空！");
      return;
    }
    if(GlobalFunctions.isEmptyobj(this.contractSealModel.code)){
      notification.error("编号不能为空");
      return;
    }
    if(GlobalFunctions.isEmptyobj(this.contractSealModel.name)){
      notification.error("名称不能为空");
      return;
    }
    if(GlobalFunctions.isEmptyobj(this.contractSealModel.departmentid)){
      notification.error("经办部门不能为空!");
      return;
    }
    let data={id: this.contractSealModel.id,//
            userid: this.contractSealModel.userid,//--操作员ID
            bill_date: this.contractSealModel.bill_date,//--申请日期
            contractsid: this.contractSealModel.contractsid,
            code: this.contractSealModel.code,//----合同编号
            name: this.contractSealModel.name,//--合同名称
            status: this.contractSealModel.status,//--状态
            percent: 0,
            contractprogressid: 0,
            message: this.contractSealModel.message,
           sourcetype: this.contractSealModel.sourcetype,
           sourceid: this.contractSealModel.sourceid,
           employeeid:this.contractSealModel.employeeid,
           sealdate:this.contractSealModel.sealdate,
           sealcount:this.contractSealModel.sealcount,
           handledept:this.contractSealModel.handledept,
           departmentid: this.contractSealModel.departmentid,
           archive:this.contractSealModel.archive,
    }
    if(this.contractSealModel.isnew) {
      ContractSeal.create(data)
        .then(() => {
          this.init(this.contractSealModel.id);
          notification.success("新增成功.");
        })
        .catch(error => {
          notification.error(error.message, error.detailedMessage);
        });
    }else{
      ContractSeal.save(data)
        .then(() => {
          notification.success("修改成功.");
        })
        .catch(error => {
          notification.error(error.message, error.detailedMessage);
        });
    }
  };
  dataDelete() {
    ContractSeal.delete({ id: this.contractSealModel.id }).then(() => {
      notification.success("删除成功.");
      this.init(0);
    }).catch(error => {
      notification.error("删除失败", error.response.data.message);
    });

  };
  billCheck(userid) {
    if(!currentUser.isAudit){
      if (this.contractSealModel.status===0  || (this.contractSealModel.status === 1 && this.contractSealModel.userid === currentUser.id)){

      }else{
        notification.error("没有权限.");
        return;
      }
    }
    if(this.contractSealModel.isnew){
      this.dataSave();
      return ;
    }
    let item = {
      bill: "contractseal",
      billtype: 0,
      billid: this.contractSealModel.id,
      next_type: 1,
      message:  "<div>同意</div>",
      userid: userid,
      billdate: this.contractSealModel.bill_date.format('YYYY-MM-DD'),
      orgunitid: this.contractSealModel.departmentid,
      status: this.contractSealModel.status,
      warnsealuser:"",
      billuserid: userid,
    }
    if (this.contractSealModel.status === 0) {
      Workflow.check(item).then(data => {
        if (data[0].flag === 0) {
          this.init(this.contractSealModel.id);
          notification.success(data[0].message);
        } else {
          notification.error(data[0].message);
        }
      })
        .catch(error => {
          notification.error(error.message, error.detailedMessage);
        });
    } else {
      CheckDialog.showModal({ bill_check: item })
        .onClose((result = {}) => {
          if (result.success) {
            this.init(this.contractSealModel.id);
          }
        }).onDismiss(() => {
        });
    }
  };
}

export default ContractSealStore;